layui.config({
    base: '/plugins/module/'
});

layui.use(['table','layer','form','dtree','cstmodules'], function () {

    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var dtree = layui.dtree;

    // 渲染表格
    var sysmgr={
        param:{
            tableColumn:[
                [
                    {type: 'checkbox'},
                    {field:'id',sort: true,title:'ID',align:'center'}
                    ,{field:'rolename', title:'角色名',align:'center'}
                    ,{field:'remark', title:'备注',align:'center'}
                    ,{field:'createtime', title:'创建时间',align:'center'}
                    ,{title:'操作', toolbar: '#roleRowBar',align:'center'}
                ]
            ],
            mainIndex:null,
            url:null,
            limit:15,
            limits: [15, 30, 60, 120]
        },

        //删除
        deleteRole:function(data){
            layer.confirm('你确定要删除【'+data.rolename+'】这个角色吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/role/deleteRole",{id:data.id},function(res){
                    if(res.code==1){
                        table.reload('roleTable');
                    }
                    layer.msg(res.info);
                })
                layer.close(index);
            })
        },

        //批量删除
        batchDelete:function  (){
            //得到选中行
            var checkStatus = table.checkStatus('roleTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要删除这些角色数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids+="ids="+item.id;
                        }else{
                            ids+="&ids="+item.id;
                        }
                    })
                    $.post("/role/batchDeleterole",ids,function(res){
                        if(res.code==1){
                            table.reload('roleTable');
                        }
                        layer.msg(res.info);
                    })
                    layer.close(index);
                });

            }else{
                layer.msg("请选中操作行")
            }
        },

        //打开修改的弹出层
        openUpdateMenuLayer:function (data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['500px','500px'],
                title:'修改角色',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/role/updateRole";
                }
            });
        },

        //打开添加的弹出层
        openAddLayer:function(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['500px','500px'],
                title:'添加角色',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    url="/role/addRole";
                }
            });
        },

        //打开分配权限的弹出层
        selectPermission:function(data){
            mainIndex=layer.open({
                type:1,
                content:$("#selectRolePermissionDiv"),
                area:['400px','480px'],
                title:'分配【'+data.rolename+'】的权限',
                skin:"layui-layer-lan",
                btn: ['<span style="font-size: 15px;"><i class="fa fa-check restbtn"></i>确认分配</span>',
                    '<span style="font-size: 15px "><i class="fa fa fa-times restbtn"></i>关闭窗口</span>'],
                yes: function(index, layero){
                    //得到树选中的所有节点
                    var permissionData = dtree.getCheckbarNodesParam("permissionTree");
                    console.log(permissionData);
                    var params="id="+data.id;
                    $.each(permissionData,function(index,item){
                        params+="&ids="+item.nodeId;
                    });
                    $.post("/role/saveRoleToken",params,function(res){
                        layer.msg(res.info);
                        layer.close(index);
                    })
                },
                btn2: function(index, layero){
                    //return false 开启该代码可禁止点击该按钮关闭
                },
                btnAlign: 'c',
                success:function(){
                    // initRoleTable(data);
                    dtree.render({
                        elem: "#permissionTree",
                        dataStyle: "layuiStyle",  //使用layui风格的数据格式
                        dataFormat: "list",  //配置data的风格为list
                        response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
                        url: "/role/initRoleMenuTreeJson?roleid="+data.id,
                        checkbar: true,
                        checkbarType: "all" // 默认就是all，其他的值为： no-all  p-casc   self  only
                    });
                }
            })
        },

    }
    mymod.initPageTableMethod(table,"roleTable","/role/loadAllRole",sysmgr.param.tableColumn,"#roleToolBar",null,sysmgr.param.limit,sysmgr.param.limits,120);

    //模糊查询
    form.on("submit(doSearch)",function(data){
        table.reload('roleTable',{
            where:data.field,
            page:{
                curr:1
            }
        });
        return false;
    });

    //监听工具条
    table.on('tool(roleTable)', function (obj) {
        var data = obj.data;
        switch(obj.event){
            case 'delete':
                sysmgr.deleteRole(data)
                break;
            case 'update':
                sysmgr.openUpdateMenuLayer(data);
                break;
            case 'selectPermission':
                sysmgr.selectPermission(data);
                break;
        }
    });

    //监听头部工具栏
    table.on('toolbar(roleTable)', function(obj){
        switch(obj.event){
            case 'add':
                sysmgr.openAddLayer();
                break;
            case 'batchdelte':
                sysmgr.batchDelete();
                break;
        }
    });

    //监听提交
    form.on('submit(doSubmit)', function(data){

        $.post(url,data.field,function(res){
            if(res.code==1){
                table.reload('roleTable');
            }
            layer.msg(res.info);
            layer.close(mainIndex);
        })
        return false;
    });
});